<template>
  <div class="container">
    <div class="title">邀请好友</div>
    <div class="content">
      <div class="conTitle">邀请好友购买商品赚佣金</div>
      <div ref="myCode" class="conCode"></div>
      <van-button type="primary" class="button but1" @click="copyCode(0)"
        >复制邀请码</van-button
      >
      <van-button type="default" class="button but2" @click="copyCode(1)"
        >复制邀请链接</van-button
      >
    </div>
  </div>
</template>

<script>
import { getUserInfo } from '../../api/api'
import QRCode from 'qrcodejs2'
export default {
  data() {
    return {
      inviteCode: '',
      code: '',
    };
  },
  methods: {
    copyCode(type) {
      let code;
      switch (type) {
        case 0:
          code = this.inviteCode;
          break;
        case 1:
          code = this.code;
          break;

        default:
          break;
      }

      this.$copyText(code).then(
         e => {
          console.log("copy arguments e:", e);
          this.$toast.success('复制成功');
        }
      );
    },
    getUser() {
      let data = {
        user_id: localStorage.getItem('userId')
      }
      getUserInfo(data).then(res => {
        this.$toast.clear()
        this.inviteCode = res.data.invite_code
        this.code = `https://www.858j.xyz/html/index.html#/login?invite_code=${res.data.invite_code}`
        this.crateQrcode()
        console.log('获取用户信息接口---', res.data)
      })
    },
    crateQrcode() {
      const qrcode = new QRCode(this.$refs.myCode, {
        text: this.code,
        width: 250,
        height: 250,
        colorDark: '#000',
        colorLight: '#fff',
      })
    },
  },
  mounted() {
    this.getUser()
  },
};
</script>

<style lang="less" scoped>
div {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
.container {
  background-color: #f1f1f1;
  min-height: 100vh;
  .title {
    width: 100%;
    height: 60px;
    color: #fff;
    text-align: center;
    line-height: 60px;
    font-size: 1.2em;
    background-color: #333333;
  }
  .content {
    text-align: center;

    .conTitle {
      margin-top: 25%;
      color: #333333;
      font-size: 1.4em;
    }

    .conCode {
      width: 250px;
      height: 250px;
      margin: 5% auto 0;
    }

    .button {
      display: block;
      width: 65%;
      margin: 0 auto;
      border-radius: 5px;
      font-size: 1em;
      letter-spacing: 1px;
    }

    .but1 {
      margin-top: 10%;
    }
    .but2 {
      margin-top: 10px;
      color: #3ace8a;
      border: 1px solid #3ace8a;
      background-color: transparent;
    }
  }
}
</style>